Geographic data in Python

GEOG 30323

March 31, 2020

What is geographic data?

  • Thus far: we’ve been working with data (largely) in tabular data frames, where each column represents data “attributes”
  • Geographic data: includes information on location - specifically, where the observation is located on the Earth’s surface

Maps!

Longitude and latitude: the basics

Geographic coordinates

  • Longitude (X) and latitude (Y) coordinates can be expressed as:
    • Degrees minutes seconds (e.g. \(97^{\circ}21'37''W\), \(32^{\circ}42'38''N\))
    • Decimal degrees (e.g. \(-97.36\), \(32.71\))

Conversion between DMS and decimal degrees:

\[DD = D + \frac{M}{60} + \frac{S}{3600}\]

Coordinate systems

  • Geographic coordinate system: coordinate system based on latitude and longitude (coordinates on a sphere)
  • Projected coordinate system: coordinate system projected onto a two-dimensional surface (coordinates on a plane)
  • Example: http://bl.ocks.org/mbostock/raw/5731632

Map projections

Original source: Mike Bostock

Tiled mapping and Web Mercator

Map projections and distortion

Examples:

Vector data

Raster data

Vector data: CSV files

  • Example: Starbucks locations in Chicago

GeoPandas

  • GeoPandas: Python package that extends pandas to represent geographic objects as GeoDataFrames

  • Key column: geometry, which represents the geometry type of the data (point, line, or polygon) and the sequence of coordinates

Making a GeoDataFrame

Making a GeoDataFrame

  • gp.GeoDataFrame(): function used to generate the GeoDataFrame, which can take an existing pandas DataFrame as its first argument

  • Parameters:
    • geometry: how to represent the data as a geographic object. We use the gp.points_from_xy() function here to “make” the geometry from existing Longitude and Latitude columns.
    • crs: a code that specifies the dataset’s coordinate reference system. Most coordinate systems can be represented by 4- or 5-digit codes (see https://spatialreference.org/)

Mapping point data

Adding a “basemap”

Adding a “basemap”

Vector data: GeoJSON

Mapping polygons

Mapping polygons

Should you map?

  • Beware the “amazing map”…
Source: Clickhole (The Onion)

Maps vs. charts

  • For discussion: which visualization is more effective for showing differences in our data?

Chart for comparison

Code to reproduce the chart

Raster data in Python

  • To work with raster data in Python, use the rasterio package

Plotting raster data

Raster data: histograms